package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ResultCode;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author cs
 * @since 2021-02-09
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id) {
        EduVideo video = videoService.getById(id);
        if (!StringUtils.isEmpty(video) && !StringUtils.isEmpty(video.getVideoSourceId())) {
            R result = vodClient.removeVideo(video.getVideoSourceId());
            if (result.getCode() == ResultCode.ERROR) {
                throw new GuliException(ResultCode.ERROR,"删除视频失败，熔断器");
            }
        }
        videoService.removeById(id);
        return R.ok();
    }

    //查询详细信息
    @GetMapping("{id}")
    public R getVideo(@PathVariable String id) {
        EduVideo eduVideo = videoService.getById(id);
        return R.ok().data("item", eduVideo);
    }

    //更新

}

